/*
 * File: Ctrl.cpp
 *
 * Code generated for Simulink model 'Ctrl'.
 *
 * Model version                  : 1.431
 * Simulink Coder version         : 8.2 (R2012a) 29-Dec-2011
 * TLC version                    : 8.2 (Dec 29 2011)
 * C/C++ source code generated on : Sat Apr 14 13:39:19 2012
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: Texas Instruments->C2000
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#include "Ctrl.h"
#include "Ctrl_private.h"

/* Named constants for Chart: '<S1>/Chart' */
#define Ctrl_CALL_EVENT                (-1L)
#define Ctrl_IN_ClosedLoopSpeed        ((uint8_T)1U)
#define Ctrl_IN_ClosedLoopTorque       ((uint8_T)2U)
#define Ctrl_IN_EncoderFail            ((uint8_T)3U)
#define Ctrl_IN_Failed                 ((uint8_T)1U)
#define Ctrl_IN_InitEncoder            ((uint8_T)4U)
#define Ctrl_IN_NO_ACTIVE_CHILD        ((uint8_T)0U)
#define Ctrl_IN_Off                    ((uint8_T)5U)
#define Ctrl_IN_Offline                ((uint8_T)2U)
#define Ctrl_IN_Online                 ((uint8_T)3U)
#define Ctrl_IN_OpenLoopCurrent        ((uint8_T)6U)
#define Ctrl_IN_OpenLoopDuty           ((uint8_T)7U)
#define Ctrl_IN_Powerup                ((uint8_T)4U)
#define Ctrl_IN_Tripper                ((uint8_T)1U)
#define Ctrl_event_FAIL                (0L)

/* Function for Chart: '<S1>/Chart' */
void CtrlModelClass::Ctrl_SetOutputs(real_T state)
{
  /* Graphical Function 'SetOutputs': '<S2>:37' */
  /* Transition: '<S2>:84' */
  /* Transition: '<S2>:172' */
  /*  All Off */
  Ctrl_B.CtrlOff = FALSE;
  Ctrl_B.CtrlOLV = FALSE;
  Ctrl_B.CtrlOLC = FALSE;
  Ctrl_B.CtrlCLT = FALSE;
  Ctrl_B.CtrlCLS = FALSE;
  Ctrl_B.CtrlCLP = FALSE;
  if (state == 0.0) {
    /* Transition: '<S2>:89' */
    /* Transition: '<S2>:119' */
    /*  Off */
    Ctrl_B.CtrlOff = TRUE;

    /* Transition: '<S2>:141' */
  } else if (state == 1.0) {
    /* Transition: '<S2>:90' */
    /* Transition: '<S2>:127' */
    /*  OLDuty */
    Ctrl_B.CtrlOLV = TRUE;

    /* Transition: '<S2>:142' */
  } else if (state == 2.0) {
    /* Transition: '<S2>:91' */
    /* Transition: '<S2>:131' */
    /*  OLCurrent */
    Ctrl_B.CtrlOLC = TRUE;

    /* Transition: '<S2>:143' */
  } else if (state == 3.0) {
    /* Transition: '<S2>:114' */
    /* Transition: '<S2>:133' */
    /*  CLTorque */
    Ctrl_B.CtrlCLT = TRUE;

    /* Transition: '<S2>:144' */
  } else {
    if (state == 4.0) {
      /* Transition: '<S2>:117' */
      /* Transition: '<S2>:135' */
      /*  CLSpeed */
      Ctrl_B.CtrlCLS = TRUE;

      /* Transition: '<S2>:145' */
    }
  }
}

/* Function for Chart: '<S1>/Chart' */
void CtrlModelClass::Ctrl_SystemLogic(void)
{
  int32_T b_previousEvent;
  boolean_T guard = FALSE;

  /* During 'SystemLogic': '<S2>:7' */
  switch (Ctrl_DWork.is_SystemLogic) {
   case Ctrl_IN_Failed:
    /* During 'Failed': '<S2>:3' */
    if (Ctrl_U.AckError) {
      /* Transition: '<S2>:12' */
      Ctrl_DWork.is_SystemLogic = Ctrl_IN_Offline;

      /* Entry 'Offline': '<S2>:1' */
      Ctrl_B.Kill = TRUE;
      Ctrl_B.ActualState = 0UL;
    }
    break;

   case Ctrl_IN_Offline:
    /* During 'Offline': '<S2>:1' */
    if (Ctrl_U.Active) {
      /* Transition: '<S2>:4' */
      Ctrl_DWork.is_SystemLogic = Ctrl_IN_Powerup;
      Ctrl_DWork.temporalCounter_i1 = 0U;

      /* Entry 'Powerup': '<S2>:166' */
      Ctrl_B.Kill = FALSE;
      Ctrl_B.ClearTrip = TRUE;
    }
    break;

   case Ctrl_IN_Online:
    /* During 'Online': '<S2>:2' */
    if (Ctrl_DWork.sfEvent == Ctrl_event_FAIL) {
      /* Transition: '<S2>:6' */
      /* Exit Internal 'Online': '<S2>:2' */
      Ctrl_DWork.is_Online = Ctrl_IN_NO_ACTIVE_CHILD;
      if ((int16_T)Ctrl_DWork.is_SystemLogic == (int16_T)Ctrl_IN_Online) {
        /* Exit 'Online': '<S2>:2' */
        Ctrl_B.ActualMode = 0UL;
        Ctrl_SetOutputs(0.0);
        Ctrl_DWork.is_SystemLogic = Ctrl_IN_NO_ACTIVE_CHILD;
      }

      if ((int16_T)Ctrl_DWork.is_SystemLogic != (int16_T)Ctrl_IN_Failed) {
        Ctrl_DWork.is_SystemLogic = Ctrl_IN_Failed;

        /* Entry 'Failed': '<S2>:3' */
        Ctrl_B.Kill = TRUE;
        Ctrl_B.ActualState = 2UL;
      }
    } else if (!Ctrl_U.Active) {
      /* Transition: '<S2>:5' */
      /* Exit Internal 'Online': '<S2>:2' */
      Ctrl_DWork.is_Online = Ctrl_IN_NO_ACTIVE_CHILD;
      if ((int16_T)Ctrl_DWork.is_SystemLogic == (int16_T)Ctrl_IN_Online) {
        /* Exit 'Online': '<S2>:2' */
        Ctrl_B.ActualMode = 0UL;
        Ctrl_SetOutputs(0.0);
        Ctrl_DWork.is_SystemLogic = Ctrl_IN_NO_ACTIVE_CHILD;
      }

      if ((int16_T)Ctrl_DWork.is_SystemLogic != (int16_T)Ctrl_IN_Offline) {
        Ctrl_DWork.is_SystemLogic = Ctrl_IN_Offline;

        /* Entry 'Offline': '<S2>:1' */
        Ctrl_B.Kill = TRUE;
        Ctrl_B.ActualState = 0UL;
      }
    } else if ((Ctrl_U.Mode == 0UL) && (!((int16_T)Ctrl_DWork.is_Online ==
                 (int16_T)Ctrl_IN_Off))) {
      /* Transition: '<S2>:25' */
      /* Exit Internal 'Online': '<S2>:2' */
      Ctrl_DWork.is_Online = Ctrl_IN_NO_ACTIVE_CHILD;
      if ((int16_T)Ctrl_DWork.is_Online != (int16_T)Ctrl_IN_Off) {
        Ctrl_DWork.is_Online = Ctrl_IN_Off;

        /* Entry 'Off': '<S2>:17' */
        Ctrl_B.Kill = FALSE;
        Ctrl_B.ActualMode = 0UL;
        Ctrl_SetOutputs(0.0);
      }
    } else {
      switch (Ctrl_DWork.is_Online) {
       case Ctrl_IN_ClosedLoopSpeed:
        break;

       case Ctrl_IN_ClosedLoopTorque:
        break;

       case Ctrl_IN_EncoderFail:
        break;

       case Ctrl_IN_InitEncoder:
        /* During 'InitEncoder': '<S2>:28' */
        if (Ctrl_U.IdxLock) {
          /* Transition: '<S2>:32' */
          if (Ctrl_U.Mode == 3UL) {
            /* Transition: '<S2>:33' */
            Ctrl_DWork.is_Online = Ctrl_IN_ClosedLoopTorque;

            /* Entry 'ClosedLoopTorque': '<S2>:20' */
            Ctrl_B.ActualMode = 3UL;
            Ctrl_SetOutputs(3.0);
          } else if (Ctrl_U.Mode == 4UL) {
            /* Transition: '<S2>:34' */
            Ctrl_DWork.is_Online = Ctrl_IN_ClosedLoopSpeed;

            /* Entry 'ClosedLoopSpeed': '<S2>:22' */
            Ctrl_B.ActualMode = 4UL;
            Ctrl_SetOutputs(4.0);
          } else {
            guard = TRUE;
          }
        } else {
          guard = TRUE;
        }
        break;

       case Ctrl_IN_Off:
        /* During 'Off': '<S2>:17' */
        if (Ctrl_U.Mode == 1UL) {
          /* Transition: '<S2>:24' */
          Ctrl_DWork.is_Online = Ctrl_IN_OpenLoopDuty;

          /* Entry 'OpenLoopDuty': '<S2>:18' */
          Ctrl_B.ActualMode = 1UL;
          Ctrl_SetOutputs(1.0);
        } else if (Ctrl_U.Mode == 2UL) {
          /* Transition: '<S2>:26' */
          Ctrl_DWork.is_Online = Ctrl_IN_OpenLoopCurrent;

          /* Entry 'OpenLoopCurrent': '<S2>:19' */
          Ctrl_B.ActualMode = 2UL;
          Ctrl_SetOutputs(2.0);
        } else {
          if (Ctrl_U.Mode > 2UL) {
            /* Transition: '<S2>:29' */
            Ctrl_DWork.is_Online = Ctrl_IN_InitEncoder;
            Ctrl_DWork.temporalCounter_i1 = 0U;
          }
        }
        break;

       case Ctrl_IN_OpenLoopCurrent:
        break;

       case Ctrl_IN_OpenLoopDuty:
        break;

       default:
        Ctrl_DWork.is_Online = Ctrl_IN_NO_ACTIVE_CHILD;
        break;
      }
    }
    break;

   case Ctrl_IN_Powerup:
    /* During 'Powerup': '<S2>:166' */
    if (Ctrl_DWork.temporalCounter_i1 >= 10000U) {
      /* Transition: '<S2>:167' */
      if ((int16_T)Ctrl_DWork.is_SystemLogic == (int16_T)Ctrl_IN_Powerup) {
        /* Exit 'Powerup': '<S2>:166' */
        Ctrl_B.ClearTrip = FALSE;
        Ctrl_DWork.is_SystemLogic = Ctrl_IN_NO_ACTIVE_CHILD;
      }

      if ((int16_T)Ctrl_DWork.is_SystemLogic != (int16_T)Ctrl_IN_Online) {
        Ctrl_DWork.is_SystemLogic = Ctrl_IN_Online;

        /* Entry 'Online': '<S2>:2' */
        Ctrl_B.ActualState = 1UL;
      }

      /* Entry Internal 'Online': '<S2>:2' */
      /* Transition: '<S2>:23' */
      if ((int16_T)Ctrl_DWork.is_Online != (int16_T)Ctrl_IN_Off) {
        Ctrl_DWork.is_Online = Ctrl_IN_Off;

        /* Entry 'Off': '<S2>:17' */
        Ctrl_B.Kill = FALSE;
        Ctrl_B.ActualMode = 0UL;
        Ctrl_SetOutputs(0.0);
      }
    }
    break;

   default:
    Ctrl_DWork.is_SystemLogic = Ctrl_IN_NO_ACTIVE_CHILD;
    break;
  }

  if (guard && (Ctrl_DWork.temporalCounter_i1 >= 60000U)) {
    /* Transition: '<S2>:164' */
    Ctrl_DWork.is_Online = Ctrl_IN_EncoderFail;

    /* Entry 'EncoderFail': '<S2>:163' */
    b_previousEvent = Ctrl_DWork.sfEvent;
    Ctrl_DWork.sfEvent = Ctrl_event_FAIL;
    if ((int16_T)Ctrl_DWork.is_active_SystemLogic != 0) {
      Ctrl_SystemLogic();
    }

    Ctrl_DWork.sfEvent = b_previousEvent;
  }
}

/* Model step function */
void CtrlModelClass::step()
{
  int32_T b_previousEvent;
  real32_T rtb_Sum_e;
  real32_T rtb_IntegralGain_n;
  real32_T rtb_Saturation_fs;
  real32_T rtb_Saturation_k;
  real32_T rtb_IntegralGain;
  real32_T rtb_Sum;
  real32_T rtb_Saturation_n;
  real32_T rtb_Switch_gt;
  real32_T rtb_IntegralGain_n_0;
  real32_T rtb_Sum_e_0;

  /* Outputs for Atomic SubSystem: '<Root>/Ctrl' */
  /* Chart: '<S1>/Chart' incorporates:
   *  Inport: '<Root>/ExtTrip'
   */
  Ctrl_DWork.presentTicks = Ctrl_M->Timing.clockTick0;
  Ctrl_DWork.elapsedTicks = Ctrl_DWork.presentTicks - Ctrl_DWork.previousTicks;
  Ctrl_DWork.previousTicks = Ctrl_DWork.presentTicks;
  if ((uint32_T)Ctrl_DWork.temporalCounter_i1 + Ctrl_DWork.elapsedTicks <=
      65535UL) {
    Ctrl_DWork.temporalCounter_i1 = (uint16_T)((uint32_T)
      Ctrl_DWork.temporalCounter_i1 + Ctrl_DWork.elapsedTicks);
  } else {
    Ctrl_DWork.temporalCounter_i1 = MAX_uint16_T;
  }

  /* Gateway: Ctrl/Chart */
  Ctrl_DWork.sfEvent = Ctrl_CALL_EVENT;

  /* During: Ctrl/Chart */
  if ((int16_T)Ctrl_DWork.is_active_c1_Ctrl == 0) {
    /* Entry: Ctrl/Chart */
    Ctrl_DWork.is_active_c1_Ctrl = 1U;

    /* Entry Internal: Ctrl/Chart */
    Ctrl_DWork.is_active_SystemLogic = 1U;

    /* Entry Internal 'SystemLogic': '<S2>:7' */
    /* Transition: '<S2>:11' */
    if ((int16_T)Ctrl_DWork.is_SystemLogic != (int16_T)Ctrl_IN_Offline) {
      Ctrl_DWork.is_SystemLogic = Ctrl_IN_Offline;

      /* Entry 'Offline': '<S2>:1' */
      Ctrl_B.Kill = TRUE;
      Ctrl_B.ActualState = 0UL;
    }

    Ctrl_DWork.is_active_FailureDetection = 1U;

    /* Entry Internal 'FailureDetection': '<S2>:176' */
    Ctrl_DWork.is_active_ExternalTrip = 1U;

    /* Entry Internal 'ExternalTrip': '<S2>:177' */
    /* Transition: '<S2>:179' */
    Ctrl_DWork.is_ExternalTrip = Ctrl_IN_Tripper;
  } else {
    if ((int16_T)Ctrl_DWork.is_active_SystemLogic != 0) {
      Ctrl_SystemLogic();
    }

    if (!(((int16_T)Ctrl_DWork.is_active_FailureDetection == 0) || ((int16_T)
          Ctrl_DWork.is_active_ExternalTrip == 0) || ((int16_T)
          Ctrl_DWork.is_ExternalTrip != (int16_T)Ctrl_IN_Tripper) ||
          (!(Ctrl_U.ExtTrip == 1)))) {
      /* During 'FailureDetection': '<S2>:176' */
      /* During 'ExternalTrip': '<S2>:177' */
      /* During 'Tripper': '<S2>:178' */
      /* Transition: '<S2>:181' */
      /* Transition: '<S2>:184' */
      b_previousEvent = Ctrl_DWork.sfEvent;
      Ctrl_DWork.sfEvent = Ctrl_event_FAIL;
      if ((int16_T)Ctrl_DWork.is_active_SystemLogic != 0) {
        Ctrl_SystemLogic();
      }

      Ctrl_DWork.sfEvent = b_previousEvent;
      Ctrl_DWork.is_ExternalTrip = Ctrl_IN_Tripper;
    }
  }

  /* End of Chart: '<S1>/Chart' */

  /* Outputs for Enabled SubSystem: '<S1>/ClosedLoopSpeed' incorporates:
   *  EnablePort: '<S3>/Enable'
   */
  if (Ctrl_B.CtrlCLS) {
    if (!Ctrl_DWork.ClosedLoopSpeed_MODE) {
      /* InitializeConditions for DiscreteIntegrator: '<S8>/Integrator' */
      Ctrl_DWork.Integrator_DSTATE_h1 = Ctrl_P.Integrator_IC;

      /* InitializeConditions for DiscreteIntegrator: '<S10>/Integrator' */
      Ctrl_DWork.Integrator_DSTATE_o = Ctrl_P.Integrator_IC_f;

      /* InitializeConditions for DiscreteIntegrator: '<S9>/Integrator' */
      Ctrl_DWork.Integrator_DSTATE_os = Ctrl_P.Integrator_IC_b;
      Ctrl_DWork.ClosedLoopSpeed_MODE = TRUE;
    }

    /* Inport: '<S3>/ElectrAngle' incorporates:
     *  Inport: '<Root>/ElAngle'
     */
    Ctrl_B.ElAngle = Ctrl_U.ElAngle;

    /* Sum: '<S3>/Sum' incorporates:
     *  Constant: '<S3>/Constant1'
     *  Inport: '<Root>/Idq'
     */
    rtb_Sum_e = Ctrl_P.Constant1_Value - Ctrl_U.Idq[0];

    /* Sum: '<S8>/Sum' incorporates:
     *  DiscreteIntegrator: '<S8>/Integrator'
     *  Gain: '<S8>/Proportional Gain'
     */
    rtb_IntegralGain_n = Ctrl_P.ProportionalGain_Gain * rtb_Sum_e +
      Ctrl_DWork.Integrator_DSTATE_h1;

    /* Saturate: '<S8>/Saturation' */
    if (rtb_IntegralGain_n >= Ctrl_P.Saturation_UpperSat) {
      rtb_Saturation_fs = Ctrl_P.Saturation_UpperSat;
    } else if (rtb_IntegralGain_n <= Ctrl_P.Saturation_LowerSat) {
      rtb_Saturation_fs = Ctrl_P.Saturation_LowerSat;
    } else {
      rtb_Saturation_fs = rtb_IntegralGain_n;
    }

    /* End of Saturate: '<S8>/Saturation' */

    /* Gain: '<S8>/Integral Gain' */
    rtb_Sum_e *= Ctrl_P.IntegralGain_Gain;

    /* Signum: '<S11>/SignPreSat' */
    if (rtb_IntegralGain_n < 0.0F) {
      rtb_IntegralGain_n_0 = -1.0F;
    } else if (rtb_IntegralGain_n > 0.0F) {
      rtb_IntegralGain_n_0 = 1.0F;
    } else if (rtb_IntegralGain_n == 0.0F) {
      rtb_IntegralGain_n_0 = 0.0F;
    } else {
      rtb_IntegralGain_n_0 = rtb_IntegralGain_n;
    }

    /* Signum: '<S11>/SignPreIntegrator' */
    if (rtb_Sum_e < 0.0F) {
      rtb_Sum_e_0 = -1.0F;
    } else if (rtb_Sum_e > 0.0F) {
      rtb_Sum_e_0 = 1.0F;
    } else if (rtb_Sum_e == 0.0F) {
      rtb_Sum_e_0 = 0.0F;
    } else {
      rtb_Sum_e_0 = rtb_Sum_e;
    }

    /* Switch: '<S8>/Switch' incorporates:
     *  Constant: '<S8>/Constant'
     *  Logic: '<S11>/AND'
     *  RelationalOperator: '<S11>/Equal'
     *  RelationalOperator: '<S11>/NotEqual'
     *  Signum: '<S11>/SignPreIntegrator'
     *  Signum: '<S11>/SignPreSat'
     */
    if ((rtb_IntegralGain_n != rtb_Saturation_fs) && (rtb_IntegralGain_n_0 ==
         rtb_Sum_e_0)) {
      rtb_Switch_gt = Ctrl_P.Constant_Value;
    } else {
      rtb_Switch_gt = rtb_Sum_e;
    }

    /* End of Switch: '<S8>/Switch' */

    /* Sum: '<S3>/Sum2' incorporates:
     *  Inport: '<Root>/RPMRef'
     *  Inport: '<Root>/SpeedRPM'
     */
    rtb_IntegralGain_n = Ctrl_U.RPMRef - Ctrl_U.SpeedRPM;

    /* Sum: '<S10>/Sum' incorporates:
     *  DiscreteIntegrator: '<S10>/Integrator'
     *  Gain: '<S10>/Proportional Gain'
     */
    rtb_Sum_e = Ctrl_P.ProportionalGain_Gain_p * rtb_IntegralGain_n +
      Ctrl_DWork.Integrator_DSTATE_o;

    /* Saturate: '<S10>/Saturation' */
    if (rtb_Sum_e >= Ctrl_P.Saturation_UpperSat_m) {
      rtb_Saturation_k = Ctrl_P.Saturation_UpperSat_m;
    } else if (rtb_Sum_e <= Ctrl_P.Saturation_LowerSat_g) {
      rtb_Saturation_k = Ctrl_P.Saturation_LowerSat_g;
    } else {
      rtb_Saturation_k = rtb_Sum_e;
    }

    /* End of Saturate: '<S10>/Saturation' */

    /* Sum: '<S3>/Sum1' incorporates:
     *  Inport: '<Root>/Idq'
     */
    rtb_IntegralGain = rtb_Saturation_k - Ctrl_U.Idq[1];

    /* Sum: '<S9>/Sum' incorporates:
     *  DiscreteIntegrator: '<S9>/Integrator'
     *  Gain: '<S9>/Proportional Gain'
     */
    rtb_Sum = Ctrl_P.ProportionalGain_Gain_n * rtb_IntegralGain +
      Ctrl_DWork.Integrator_DSTATE_os;

    /* Saturate: '<S9>/Saturation' */
    if (rtb_Sum >= Ctrl_P.Saturation_UpperSat_a) {
      rtb_Saturation_n = Ctrl_P.Saturation_UpperSat_a;
    } else if (rtb_Sum <= Ctrl_P.Saturation_LowerSat_k) {
      rtb_Saturation_n = Ctrl_P.Saturation_LowerSat_k;
    } else {
      rtb_Saturation_n = rtb_Sum;
    }

    /* End of Saturate: '<S9>/Saturation' */

    /* Gain: '<S9>/Integral Gain' */
    rtb_IntegralGain *= Ctrl_P.IntegralGain_Gain_m;

    /* Gain: '<S10>/Integral Gain' */
    rtb_IntegralGain_n *= Ctrl_P.IntegralGain_Gain_i;

    /* Gain: '<S3>/StupidMerge' */
    Ctrl_B.Vd = Ctrl_P.StupidMerge_Gain * rtb_Saturation_fs;

    /* Gain: '<S3>/StupidMerge1' */
    Ctrl_B.Vq = Ctrl_P.StupidMerge1_Gain * rtb_Saturation_n;

    /* Update for DiscreteIntegrator: '<S8>/Integrator' */
    Ctrl_DWork.Integrator_DSTATE_h1 += Ctrl_P.Integrator_gainval * rtb_Switch_gt;

    /* Signum: '<S13>/SignPreSat' */
    if (rtb_Sum_e < 0.0F) {
      rtb_Sum_e_0 = -1.0F;
    } else if (rtb_Sum_e > 0.0F) {
      rtb_Sum_e_0 = 1.0F;
    } else if (rtb_Sum_e == 0.0F) {
      rtb_Sum_e_0 = 0.0F;
    } else {
      rtb_Sum_e_0 = rtb_Sum_e;
    }

    /* Signum: '<S13>/SignPreIntegrator' */
    if (rtb_IntegralGain_n < 0.0F) {
      rtb_IntegralGain_n_0 = -1.0F;
    } else if (rtb_IntegralGain_n > 0.0F) {
      rtb_IntegralGain_n_0 = 1.0F;
    } else if (rtb_IntegralGain_n == 0.0F) {
      rtb_IntegralGain_n_0 = 0.0F;
    } else {
      rtb_IntegralGain_n_0 = rtb_IntegralGain_n;
    }

    /* Switch: '<S10>/Switch' incorporates:
     *  Constant: '<S10>/Constant'
     *  Logic: '<S13>/AND'
     *  RelationalOperator: '<S13>/Equal'
     *  RelationalOperator: '<S13>/NotEqual'
     *  Signum: '<S13>/SignPreIntegrator'
     *  Signum: '<S13>/SignPreSat'
     */
    if ((rtb_Sum_e != rtb_Saturation_k) && (rtb_Sum_e_0 == rtb_IntegralGain_n_0))
    {
      rtb_IntegralGain_n = Ctrl_P.Constant_Value_gp;
    }

    /* End of Switch: '<S10>/Switch' */

    /* Update for DiscreteIntegrator: '<S10>/Integrator' */
    Ctrl_DWork.Integrator_DSTATE_o += Ctrl_P.Integrator_gainval_a *
      rtb_IntegralGain_n;

    /* Signum: '<S12>/SignPreSat' */
    if (rtb_Sum < 0.0F) {
      rtb_IntegralGain_n_0 = -1.0F;
    } else if (rtb_Sum > 0.0F) {
      rtb_IntegralGain_n_0 = 1.0F;
    } else if (rtb_Sum == 0.0F) {
      rtb_IntegralGain_n_0 = 0.0F;
    } else {
      rtb_IntegralGain_n_0 = rtb_Sum;
    }

    /* Signum: '<S12>/SignPreIntegrator' */
    if (rtb_IntegralGain < 0.0F) {
      rtb_Switch_gt = -1.0F;
    } else if (rtb_IntegralGain > 0.0F) {
      rtb_Switch_gt = 1.0F;
    } else if (rtb_IntegralGain == 0.0F) {
      rtb_Switch_gt = 0.0F;
    } else {
      rtb_Switch_gt = rtb_IntegralGain;
    }

    /* Switch: '<S9>/Switch' incorporates:
     *  Constant: '<S9>/Constant'
     *  Logic: '<S12>/AND'
     *  RelationalOperator: '<S12>/Equal'
     *  RelationalOperator: '<S12>/NotEqual'
     *  Signum: '<S12>/SignPreIntegrator'
     *  Signum: '<S12>/SignPreSat'
     */
    if ((rtb_Sum != rtb_Saturation_n) && (rtb_IntegralGain_n_0 == rtb_Switch_gt))
    {
      rtb_IntegralGain = Ctrl_P.Constant_Value_g;
    }

    /* End of Switch: '<S9>/Switch' */

    /* Update for DiscreteIntegrator: '<S9>/Integrator' */
    Ctrl_DWork.Integrator_DSTATE_os += Ctrl_P.Integrator_gainval_j *
      rtb_IntegralGain;
  } else {
    if (Ctrl_DWork.ClosedLoopSpeed_MODE) {
      Ctrl_DWork.ClosedLoopSpeed_MODE = FALSE;
    }
  }

  /* End of Outputs for SubSystem: '<S1>/ClosedLoopSpeed' */

  /* Outputs for Enabled SubSystem: '<S1>/ClosedLoopTorque' incorporates:
   *  EnablePort: '<S4>/Enable'
   */
  if (Ctrl_B.CtrlCLT) {
    if (!Ctrl_DWork.ClosedLoopTorque_MODE) {
      /* InitializeConditions for DiscreteIntegrator: '<S14>/Integrator' */
      Ctrl_DWork.Integrator_DSTATE_h = Ctrl_P.Integrator_IC_a;

      /* InitializeConditions for DiscreteIntegrator: '<S15>/Integrator' */
      Ctrl_DWork.Integrator_DSTATE_k = Ctrl_P.Integrator_IC_j;
      Ctrl_DWork.ClosedLoopTorque_MODE = TRUE;
    }

    /* Inport: '<S4>/ElectrAngle' incorporates:
     *  Inport: '<Root>/ElAngle'
     */
    Ctrl_B.ElAngle = Ctrl_U.ElAngle;

    /* Sum: '<S4>/Sum' incorporates:
     *  Constant: '<S4>/Constant1'
     *  Inport: '<Root>/Idq'
     */
    rtb_Sum_e = Ctrl_P.Constant1_Value_m - Ctrl_U.Idq[0];

    /* Sum: '<S14>/Sum' incorporates:
     *  DiscreteIntegrator: '<S14>/Integrator'
     *  Gain: '<S14>/Proportional Gain'
     */
    rtb_IntegralGain_n = Ctrl_P.ProportionalGain_Gain_e * rtb_Sum_e +
      Ctrl_DWork.Integrator_DSTATE_h;

    /* Saturate: '<S14>/Saturation' */
    if (rtb_IntegralGain_n >= Ctrl_P.Saturation_UpperSat_l) {
      rtb_Saturation_fs = Ctrl_P.Saturation_UpperSat_l;
    } else if (rtb_IntegralGain_n <= Ctrl_P.Saturation_LowerSat_a) {
      rtb_Saturation_fs = Ctrl_P.Saturation_LowerSat_a;
    } else {
      rtb_Saturation_fs = rtb_IntegralGain_n;
    }

    /* End of Saturate: '<S14>/Saturation' */

    /* Gain: '<S14>/Integral Gain' */
    rtb_Sum_e *= Ctrl_P.IntegralGain_Gain_mu;

    /* Signum: '<S16>/SignPreSat' */
    if (rtb_IntegralGain_n < 0.0F) {
      rtb_IntegralGain_n_0 = -1.0F;
    } else if (rtb_IntegralGain_n > 0.0F) {
      rtb_IntegralGain_n_0 = 1.0F;
    } else if (rtb_IntegralGain_n == 0.0F) {
      rtb_IntegralGain_n_0 = 0.0F;
    } else {
      rtb_IntegralGain_n_0 = rtb_IntegralGain_n;
    }

    /* Signum: '<S16>/SignPreIntegrator' */
    if (rtb_Sum_e < 0.0F) {
      rtb_Sum_e_0 = -1.0F;
    } else if (rtb_Sum_e > 0.0F) {
      rtb_Sum_e_0 = 1.0F;
    } else if (rtb_Sum_e == 0.0F) {
      rtb_Sum_e_0 = 0.0F;
    } else {
      rtb_Sum_e_0 = rtb_Sum_e;
    }

    /* Switch: '<S14>/Switch' incorporates:
     *  Constant: '<S14>/Constant'
     *  Logic: '<S16>/AND'
     *  RelationalOperator: '<S16>/Equal'
     *  RelationalOperator: '<S16>/NotEqual'
     *  Signum: '<S16>/SignPreIntegrator'
     *  Signum: '<S16>/SignPreSat'
     */
    if ((rtb_IntegralGain_n != rtb_Saturation_fs) && (rtb_IntegralGain_n_0 ==
         rtb_Sum_e_0)) {
      rtb_Switch_gt = Ctrl_P.Constant_Value_c;
    } else {
      rtb_Switch_gt = rtb_Sum_e;
    }

    /* End of Switch: '<S14>/Switch' */

    /* Sum: '<S4>/Sum1' incorporates:
     *  Inport: '<Root>/Idq'
     *  Inport: '<Root>/Iref'
     */
    rtb_IntegralGain_n = Ctrl_U.Iref - Ctrl_U.Idq[1];

    /* Sum: '<S15>/Sum' incorporates:
     *  DiscreteIntegrator: '<S15>/Integrator'
     *  Gain: '<S15>/Proportional Gain'
     */
    rtb_Sum_e = Ctrl_P.ProportionalGain_Gain_b * rtb_IntegralGain_n +
      Ctrl_DWork.Integrator_DSTATE_k;

    /* Saturate: '<S15>/Saturation' */
    if (rtb_Sum_e >= Ctrl_P.Saturation_UpperSat_b) {
      rtb_Saturation_k = Ctrl_P.Saturation_UpperSat_b;
    } else if (rtb_Sum_e <= Ctrl_P.Saturation_LowerSat_e) {
      rtb_Saturation_k = Ctrl_P.Saturation_LowerSat_e;
    } else {
      rtb_Saturation_k = rtb_Sum_e;
    }

    /* End of Saturate: '<S15>/Saturation' */

    /* Gain: '<S15>/Integral Gain' */
    rtb_IntegralGain_n *= Ctrl_P.IntegralGain_Gain_h;

    /* Gain: '<S4>/StupidMerge' */
    Ctrl_B.Vd = Ctrl_P.StupidMerge_Gain_c * rtb_Saturation_fs;

    /* Gain: '<S4>/StupidMerge1' */
    Ctrl_B.Vq = Ctrl_P.StupidMerge1_Gain_l * rtb_Saturation_k;

    /* Update for DiscreteIntegrator: '<S14>/Integrator' */
    Ctrl_DWork.Integrator_DSTATE_h += Ctrl_P.Integrator_gainval_g *
      rtb_Switch_gt;

    /* Signum: '<S17>/SignPreSat' */
    if (rtb_Sum_e < 0.0F) {
      rtb_Sum_e_0 = -1.0F;
    } else if (rtb_Sum_e > 0.0F) {
      rtb_Sum_e_0 = 1.0F;
    } else if (rtb_Sum_e == 0.0F) {
      rtb_Sum_e_0 = 0.0F;
    } else {
      rtb_Sum_e_0 = rtb_Sum_e;
    }

    /* Signum: '<S17>/SignPreIntegrator' */
    if (rtb_IntegralGain_n < 0.0F) {
      rtb_IntegralGain_n_0 = -1.0F;
    } else if (rtb_IntegralGain_n > 0.0F) {
      rtb_IntegralGain_n_0 = 1.0F;
    } else if (rtb_IntegralGain_n == 0.0F) {
      rtb_IntegralGain_n_0 = 0.0F;
    } else {
      rtb_IntegralGain_n_0 = rtb_IntegralGain_n;
    }

    /* Switch: '<S15>/Switch' incorporates:
     *  Constant: '<S15>/Constant'
     *  Logic: '<S17>/AND'
     *  RelationalOperator: '<S17>/Equal'
     *  RelationalOperator: '<S17>/NotEqual'
     *  Signum: '<S17>/SignPreIntegrator'
     *  Signum: '<S17>/SignPreSat'
     */
    if ((rtb_Sum_e != rtb_Saturation_k) && (rtb_Sum_e_0 == rtb_IntegralGain_n_0))
    {
      rtb_IntegralGain_n = Ctrl_P.Constant_Value_k;
    }

    /* End of Switch: '<S15>/Switch' */

    /* Update for DiscreteIntegrator: '<S15>/Integrator' */
    Ctrl_DWork.Integrator_DSTATE_k += Ctrl_P.Integrator_gainval_an *
      rtb_IntegralGain_n;
  } else {
    if (Ctrl_DWork.ClosedLoopTorque_MODE) {
      Ctrl_DWork.ClosedLoopTorque_MODE = FALSE;
    }
  }

  /* End of Outputs for SubSystem: '<S1>/ClosedLoopTorque' */

  /* Outputs for Enabled SubSystem: '<S1>/Off' incorporates:
   *  EnablePort: '<S5>/Enable'
   */
  if (Ctrl_B.CtrlOff) {
    /* Constant: '<S5>/Constant1' */
    Ctrl_B.Vq = Ctrl_P.Constant1_Value_k;

    /* Constant: '<S5>/Constant2' */
    Ctrl_B.Vd = Ctrl_P.Constant2_Value;

    /* Constant: '<S5>/Constant3' */
    Ctrl_B.ElAngle = Ctrl_P.Constant3_Value;
  }

  /* End of Outputs for SubSystem: '<S1>/Off' */

  /* Outputs for Enabled SubSystem: '<S1>/OpenLoopVoltage' incorporates:
   *  EnablePort: '<S7>/Enable'
   */
  if (Ctrl_B.CtrlOLV) {
    if (!Ctrl_DWork.OpenLoopVoltage_MODE) {
      /* InitializeConditions for DiscreteIntegrator: '<S25>/Discrete-Time Integrator' */
      Ctrl_DWork.DiscreteTimeIntegrator_DSTATE =
        Ctrl_P.DiscreteTimeIntegrator_IC_k;

      /* InitializeConditions for RateLimiter: '<S25>/Rate Limiter' */
      Ctrl_DWork.PrevY = Ctrl_P.RateLimiter_IC_f;
      Ctrl_DWork.OpenLoopVoltage_MODE = TRUE;
    }

    /* RateLimiter: '<S25>/Rate Limiter' incorporates:
     *  Inport: '<Root>/RPMRef'
     */
    rtb_Sum_e = Ctrl_U.RPMRef - Ctrl_DWork.PrevY;
    if (rtb_Sum_e > Ctrl_P.RateLimiter_RisingLim_e) {
      rtb_Sum_e = Ctrl_DWork.PrevY + Ctrl_P.RateLimiter_RisingLim_e;
    } else if (rtb_Sum_e < Ctrl_P.RateLimiter_FallingLim_d) {
      rtb_Sum_e = Ctrl_DWork.PrevY + Ctrl_P.RateLimiter_FallingLim_d;
    } else {
      rtb_Sum_e = Ctrl_U.RPMRef;
    }

    Ctrl_DWork.PrevY = rtb_Sum_e;

    /* End of RateLimiter: '<S25>/Rate Limiter' */

    /* Saturate: '<S25>/Saturation' incorporates:
     *  DiscreteIntegrator: '<S25>/Discrete-Time Integrator'
     */
    if (Ctrl_DWork.DiscreteTimeIntegrator_DSTATE >= Ctrl_P.Saturation_UpperSat_i)
    {
      Ctrl_B.ElAngle = Ctrl_P.Saturation_UpperSat_i;
    } else if (Ctrl_DWork.DiscreteTimeIntegrator_DSTATE <=
               Ctrl_P.Saturation_LowerSat_gd) {
      Ctrl_B.ElAngle = Ctrl_P.Saturation_LowerSat_gd;
    } else {
      Ctrl_B.ElAngle = Ctrl_DWork.DiscreteTimeIntegrator_DSTATE;
    }

    /* End of Saturate: '<S25>/Saturation' */

    /* Constant: '<S7>/Constant1' */
    Ctrl_B.Vq = Ctrl_P.Constant1_Value_h;

    /* Inport: '<S7>/Vdref' incorporates:
     *  Inport: '<Root>/Vdref'
     */
    Ctrl_B.Vd = Ctrl_U.Vd;

    /* Update for DiscreteIntegrator: '<S25>/Discrete-Time Integrator' incorporates:
     *  Constant: '<S26>/Constant'
     *  Constant: '<S27>/Constant'
     *  DiscreteIntegrator: '<S25>/Discrete-Time Integrator'
     *  Gain: '<S25>/Gain'
     *  Gain: '<S25>/Gain1'
     *  Gain: '<S25>/rps'
     *  Gain: '<S25>/rps1'
     *  RelationalOperator: '<S26>/Compare'
     *  RelationalOperator: '<S27>/Compare'
     *  Sum: '<S25>/Add'
     */
    Ctrl_DWork.DiscreteTimeIntegrator_DSTATE += ((Ctrl_P.rps_Gain_d * rtb_Sum_e *
      Ctrl_P.rps1_Gain_i - (real32_T)(Ctrl_DWork.DiscreteTimeIntegrator_DSTATE >
      Ctrl_P.Constant_Value_j) * (real32_T)Ctrl_P.Gain_Gain_b * 128.0F) +
      (real32_T)(Ctrl_DWork.DiscreteTimeIntegrator_DSTATE <
                 Ctrl_P.Constant_Value_f) * (real32_T)Ctrl_P.Gain1_Gain_j *
      128.0F) * Ctrl_P.DiscreteTimeIntegrator_gainva_h;
  } else {
    if (Ctrl_DWork.OpenLoopVoltage_MODE) {
      Ctrl_DWork.OpenLoopVoltage_MODE = FALSE;
    }
  }

  /* End of Outputs for SubSystem: '<S1>/OpenLoopVoltage' */

  /* Outputs for Enabled SubSystem: '<S1>/OpenLoopCurrent' incorporates:
   *  EnablePort: '<S6>/Enable'
   */
  if (Ctrl_B.CtrlOLC) {
    if (!Ctrl_DWork.OpenLoopCurrent_MODE) {
      /* InitializeConditions for DiscreteIntegrator: '<S18>/Discrete-Time Integrator' */
      Ctrl_DWork.DiscreteTimeIntegrator_DSTATE_n =
        Ctrl_P.DiscreteTimeIntegrator_IC;

      /* InitializeConditions for RateLimiter: '<S18>/Rate Limiter' */
      Ctrl_DWork.PrevY_p = Ctrl_P.RateLimiter_IC;

      /* InitializeConditions for DiscreteIntegrator: '<S19>/Integrator' */
      Ctrl_DWork.Integrator_DSTATE = Ctrl_P.Integrator_IC_m;

      /* InitializeConditions for DiscreteIntegrator: '<S20>/Integrator' */
      Ctrl_DWork.Integrator_DSTATE_f = Ctrl_P.Integrator_IC_n;
      Ctrl_DWork.OpenLoopCurrent_MODE = TRUE;
    }

    /* RateLimiter: '<S18>/Rate Limiter' incorporates:
     *  Inport: '<Root>/RPMRef'
     */
    rtb_Sum_e = Ctrl_U.RPMRef - Ctrl_DWork.PrevY_p;
    if (rtb_Sum_e > Ctrl_P.RateLimiter_RisingLim) {
      rtb_Sum_e = Ctrl_DWork.PrevY_p + Ctrl_P.RateLimiter_RisingLim;
    } else if (rtb_Sum_e < Ctrl_P.RateLimiter_FallingLim) {
      rtb_Sum_e = Ctrl_DWork.PrevY_p + Ctrl_P.RateLimiter_FallingLim;
    } else {
      rtb_Sum_e = Ctrl_U.RPMRef;
    }

    Ctrl_DWork.PrevY_p = rtb_Sum_e;

    /* End of RateLimiter: '<S18>/Rate Limiter' */

    /* Gain: '<S18>/rps1' incorporates:
     *  Gain: '<S18>/rps'
     */
    rtb_IntegralGain_n = Ctrl_P.rps_Gain * rtb_Sum_e * Ctrl_P.rps1_Gain;

    /* Saturate: '<S18>/Saturation' incorporates:
     *  DiscreteIntegrator: '<S18>/Discrete-Time Integrator'
     */
    if (Ctrl_DWork.DiscreteTimeIntegrator_DSTATE_n >=
        Ctrl_P.Saturation_UpperSat_f) {
      Ctrl_B.ElAngle = Ctrl_P.Saturation_UpperSat_f;
    } else if (Ctrl_DWork.DiscreteTimeIntegrator_DSTATE_n <=
               Ctrl_P.Saturation_LowerSat_gl) {
      Ctrl_B.ElAngle = Ctrl_P.Saturation_LowerSat_gl;
    } else {
      Ctrl_B.ElAngle = Ctrl_DWork.DiscreteTimeIntegrator_DSTATE_n;
    }

    /* End of Saturate: '<S18>/Saturation' */

    /* Sum: '<S6>/Sum' incorporates:
     *  Inport: '<Root>/Idq'
     *  Inport: '<Root>/Iref'
     */
    rtb_Sum_e = Ctrl_U.Iref - Ctrl_U.Idq[0];

    /* Sum: '<S19>/Sum' incorporates:
     *  DiscreteIntegrator: '<S19>/Integrator'
     *  Gain: '<S19>/Proportional Gain'
     */
    rtb_Saturation_fs = Ctrl_P.ProportionalGain_Gain_i * rtb_Sum_e +
      Ctrl_DWork.Integrator_DSTATE;

    /* Saturate: '<S19>/Saturation' */
    if (rtb_Saturation_fs >= Ctrl_P.Saturation_UpperSat_d) {
      rtb_Switch_gt = Ctrl_P.Saturation_UpperSat_d;
    } else if (rtb_Saturation_fs <= Ctrl_P.Saturation_LowerSat_ej) {
      rtb_Switch_gt = Ctrl_P.Saturation_LowerSat_ej;
    } else {
      rtb_Switch_gt = rtb_Saturation_fs;
    }

    /* End of Saturate: '<S19>/Saturation' */

    /* Gain: '<S19>/Integral Gain' */
    rtb_Sum_e *= Ctrl_P.IntegralGain_Gain_l;

    /* Signum: '<S23>/SignPreSat' */
    if (rtb_Saturation_fs < 0.0F) {
      rtb_IntegralGain_n_0 = -1.0F;
    } else if (rtb_Saturation_fs > 0.0F) {
      rtb_IntegralGain_n_0 = 1.0F;
    } else if (rtb_Saturation_fs == 0.0F) {
      rtb_IntegralGain_n_0 = 0.0F;
    } else {
      rtb_IntegralGain_n_0 = rtb_Saturation_fs;
    }

    /* Signum: '<S23>/SignPreIntegrator' */
    if (rtb_Sum_e < 0.0F) {
      rtb_Sum_e_0 = -1.0F;
    } else if (rtb_Sum_e > 0.0F) {
      rtb_Sum_e_0 = 1.0F;
    } else if (rtb_Sum_e == 0.0F) {
      rtb_Sum_e_0 = 0.0F;
    } else {
      rtb_Sum_e_0 = rtb_Sum_e;
    }

    /* Switch: '<S19>/Switch' incorporates:
     *  Constant: '<S19>/Constant'
     *  Logic: '<S23>/AND'
     *  RelationalOperator: '<S23>/Equal'
     *  RelationalOperator: '<S23>/NotEqual'
     *  Signum: '<S23>/SignPreIntegrator'
     *  Signum: '<S23>/SignPreSat'
     */
    if ((rtb_Saturation_fs != rtb_Switch_gt) && (rtb_IntegralGain_n_0 ==
         rtb_Sum_e_0)) {
      rtb_Saturation_k = Ctrl_P.Constant_Value_p0;
    } else {
      rtb_Saturation_k = rtb_Sum_e;
    }

    /* End of Switch: '<S19>/Switch' */

    /* Sum: '<S6>/Sum1' incorporates:
     *  Constant: '<S6>/Constant1'
     *  Inport: '<Root>/Idq'
     */
    rtb_Sum_e = Ctrl_P.Constant1_Value_e - Ctrl_U.Idq[1];

    /* Sum: '<S20>/Sum' incorporates:
     *  DiscreteIntegrator: '<S20>/Integrator'
     *  Gain: '<S20>/Proportional Gain'
     */
    rtb_Saturation_fs = Ctrl_P.ProportionalGain_Gain_a * rtb_Sum_e +
      Ctrl_DWork.Integrator_DSTATE_f;

    /* Saturate: '<S20>/Saturation' */
    if (rtb_Saturation_fs >= Ctrl_P.Saturation_UpperSat_as) {
      rtb_IntegralGain = Ctrl_P.Saturation_UpperSat_as;
    } else if (rtb_Saturation_fs <= Ctrl_P.Saturation_LowerSat_av) {
      rtb_IntegralGain = Ctrl_P.Saturation_LowerSat_av;
    } else {
      rtb_IntegralGain = rtb_Saturation_fs;
    }

    /* End of Saturate: '<S20>/Saturation' */

    /* Gain: '<S20>/Integral Gain' */
    rtb_Sum_e *= Ctrl_P.IntegralGain_Gain_le;

    /* Gain: '<S6>/StupidMerge' */
    Ctrl_B.Vd = Ctrl_P.StupidMerge_Gain_n * rtb_Switch_gt;

    /* Gain: '<S6>/StupidMerge1' */
    Ctrl_B.Vq = Ctrl_P.StupidMerge1_Gain_e * rtb_IntegralGain;

    /* Update for DiscreteIntegrator: '<S18>/Discrete-Time Integrator' incorporates:
     *  Constant: '<S21>/Constant'
     *  Constant: '<S22>/Constant'
     *  DiscreteIntegrator: '<S18>/Discrete-Time Integrator'
     *  Gain: '<S18>/Gain'
     *  Gain: '<S18>/Gain1'
     *  RelationalOperator: '<S21>/Compare'
     *  RelationalOperator: '<S22>/Compare'
     *  Sum: '<S18>/Add'
     */
    Ctrl_DWork.DiscreteTimeIntegrator_DSTATE_n += ((rtb_IntegralGain_n -
      (real32_T)(Ctrl_DWork.DiscreteTimeIntegrator_DSTATE_n >
                 Ctrl_P.Constant_Value_p) * (real32_T)Ctrl_P.Gain_Gain * 128.0F)
      + (real32_T)(Ctrl_DWork.DiscreteTimeIntegrator_DSTATE_n <
                   Ctrl_P.Constant_Value_ka) * (real32_T)Ctrl_P.Gain1_Gain *
      128.0F) * Ctrl_P.DiscreteTimeIntegrator_gainval;

    /* Update for DiscreteIntegrator: '<S19>/Integrator' */
    Ctrl_DWork.Integrator_DSTATE += Ctrl_P.Integrator_gainval_e *
      rtb_Saturation_k;

    /* Signum: '<S24>/SignPreSat' */
    if (rtb_Saturation_fs < 0.0F) {
      rtb_IntegralGain_n_0 = -1.0F;
    } else if (rtb_Saturation_fs > 0.0F) {
      rtb_IntegralGain_n_0 = 1.0F;
    } else if (rtb_Saturation_fs == 0.0F) {
      rtb_IntegralGain_n_0 = 0.0F;
    } else {
      rtb_IntegralGain_n_0 = rtb_Saturation_fs;
    }

    /* Signum: '<S24>/SignPreIntegrator' */
    if (rtb_Sum_e < 0.0F) {
      rtb_Sum_e_0 = -1.0F;
    } else if (rtb_Sum_e > 0.0F) {
      rtb_Sum_e_0 = 1.0F;
    } else if (rtb_Sum_e == 0.0F) {
      rtb_Sum_e_0 = 0.0F;
    } else {
      rtb_Sum_e_0 = rtb_Sum_e;
    }

    /* Switch: '<S20>/Switch' incorporates:
     *  Constant: '<S20>/Constant'
     *  Logic: '<S24>/AND'
     *  RelationalOperator: '<S24>/Equal'
     *  RelationalOperator: '<S24>/NotEqual'
     *  Signum: '<S24>/SignPreIntegrator'
     *  Signum: '<S24>/SignPreSat'
     */
    if ((rtb_Saturation_fs != rtb_IntegralGain) && (rtb_IntegralGain_n_0 ==
         rtb_Sum_e_0)) {
      rtb_Sum_e = Ctrl_P.Constant_Value_cy;
    }

    /* End of Switch: '<S20>/Switch' */

    /* Update for DiscreteIntegrator: '<S20>/Integrator' */
    Ctrl_DWork.Integrator_DSTATE_f += Ctrl_P.Integrator_gainval_i * rtb_Sum_e;
  } else {
    if (Ctrl_DWork.OpenLoopCurrent_MODE) {
      Ctrl_DWork.OpenLoopCurrent_MODE = FALSE;
    }
  }

  /* End of Outputs for SubSystem: '<S1>/OpenLoopCurrent' */
  /* End of Outputs for SubSystem: '<Root>/Ctrl' */

  /* Outport: '<Root>/Kill' */
  Ctrl_Y.Kill = Ctrl_B.Kill;

  /* Outport: '<Root>/ActualState' */
  Ctrl_Y.ActualState = Ctrl_B.ActualState;

  /* Outport: '<Root>/ActualMode' */
  Ctrl_Y.ActualMode = Ctrl_B.ActualMode;

  /* Outport: '<Root>/Cleartrip' */
  Ctrl_Y.Cleartrip = Ctrl_B.ClearTrip;

  /* Outport: '<Root>/Vd' */
  Ctrl_Y.Vd = Ctrl_B.Vd;

  /* Outport: '<Root>/Vq' */
  Ctrl_Y.Vq = Ctrl_B.Vq;

  /* Outport: '<Root>/ElAngleb' */
  Ctrl_Y.ElAngleb = Ctrl_B.ElAngle;

  /* Update absolute time for base rate */
  /* The "clockTick0" counts the number of times the code of this task has
   * been executed. The resolution of this integer timer is 5.0E-5, which is the step size
   * of the task. Size of "clockTick0" ensures timer will not overflow during the
   * application lifespan selected.
   */
  Ctrl_M->Timing.clockTick0++;
}

/* Model initialize function */
void CtrlModelClass::initialize()
{
  /* Registration code */

  /* initialize real-time model */
  (void) memset((void *)Ctrl_M, 0,
                sizeof(RT_MODEL_Ctrl));

  /* block I/O */
  (void) memset(((void *) &Ctrl_B), 0,
                sizeof(BlockIO_Ctrl));

  /* states (dwork) */
  (void) memset((void *)&Ctrl_DWork, 0,
                sizeof(D_Work_Ctrl));

  /* external inputs */
  (void) memset((void *)&Ctrl_U, 0,
                sizeof(ExternalInputs_Ctrl));

  /* external outputs */
  (void) memset((void *)&Ctrl_Y, 0,
                sizeof(ExternalOutputs_Ctrl));

  /* Start for Atomic SubSystem: '<Root>/Ctrl' */
  /* Start for Enabled SubSystem: '<S1>/ClosedLoopSpeed' */
  /* InitializeConditions for DiscreteIntegrator: '<S8>/Integrator' */
  Ctrl_DWork.Integrator_DSTATE_h1 = Ctrl_P.Integrator_IC;

  /* InitializeConditions for DiscreteIntegrator: '<S10>/Integrator' */
  Ctrl_DWork.Integrator_DSTATE_o = Ctrl_P.Integrator_IC_f;

  /* InitializeConditions for DiscreteIntegrator: '<S9>/Integrator' */
  Ctrl_DWork.Integrator_DSTATE_os = Ctrl_P.Integrator_IC_b;

  /* End of Start for SubSystem: '<S1>/ClosedLoopSpeed' */

  /* Start for Enabled SubSystem: '<S1>/ClosedLoopTorque' */
  /* InitializeConditions for DiscreteIntegrator: '<S14>/Integrator' */
  Ctrl_DWork.Integrator_DSTATE_h = Ctrl_P.Integrator_IC_a;

  /* InitializeConditions for DiscreteIntegrator: '<S15>/Integrator' */
  Ctrl_DWork.Integrator_DSTATE_k = Ctrl_P.Integrator_IC_j;

  /* End of Start for SubSystem: '<S1>/ClosedLoopTorque' */

  /* Start for Enabled SubSystem: '<S1>/OpenLoopVoltage' */
  /* InitializeConditions for DiscreteIntegrator: '<S25>/Discrete-Time Integrator' */
  Ctrl_DWork.DiscreteTimeIntegrator_DSTATE = Ctrl_P.DiscreteTimeIntegrator_IC_k;

  /* InitializeConditions for RateLimiter: '<S25>/Rate Limiter' */
  Ctrl_DWork.PrevY = Ctrl_P.RateLimiter_IC_f;

  /* End of Start for SubSystem: '<S1>/OpenLoopVoltage' */

  /* Start for Enabled SubSystem: '<S1>/OpenLoopCurrent' */
  /* InitializeConditions for DiscreteIntegrator: '<S18>/Discrete-Time Integrator' */
  Ctrl_DWork.DiscreteTimeIntegrator_DSTATE_n = Ctrl_P.DiscreteTimeIntegrator_IC;

  /* InitializeConditions for RateLimiter: '<S18>/Rate Limiter' */
  Ctrl_DWork.PrevY_p = Ctrl_P.RateLimiter_IC;

  /* InitializeConditions for DiscreteIntegrator: '<S19>/Integrator' */
  Ctrl_DWork.Integrator_DSTATE = Ctrl_P.Integrator_IC_m;

  /* InitializeConditions for DiscreteIntegrator: '<S20>/Integrator' */
  Ctrl_DWork.Integrator_DSTATE_f = Ctrl_P.Integrator_IC_n;

  /* End of Start for SubSystem: '<S1>/OpenLoopCurrent' */
  /* End of Start for SubSystem: '<Root>/Ctrl' */

  /* InitializeConditions for Atomic SubSystem: '<Root>/Ctrl' */
  /* InitializeConditions for Chart: '<S1>/Chart' */
  Ctrl_DWork.sfEvent = Ctrl_CALL_EVENT;
  Ctrl_DWork.is_active_FailureDetection = 0U;
  Ctrl_DWork.is_active_ExternalTrip = 0U;
  Ctrl_DWork.is_ExternalTrip = 0U;
  Ctrl_DWork.is_active_SystemLogic = 0U;
  Ctrl_DWork.is_SystemLogic = 0U;
  Ctrl_DWork.is_Online = 0U;
  Ctrl_DWork.is_active_c1_Ctrl = 0U;
  Ctrl_B.Kill = FALSE;
  Ctrl_B.ActualState = 0UL;
  Ctrl_B.ActualMode = 0UL;
  Ctrl_B.ClearTrip = FALSE;
  Ctrl_B.CtrlOff = FALSE;
  Ctrl_B.CtrlOLV = FALSE;
  Ctrl_B.CtrlOLC = FALSE;
  Ctrl_B.CtrlCLT = FALSE;
  Ctrl_B.CtrlCLS = FALSE;
  Ctrl_B.CtrlCLP = FALSE;
  Ctrl_DWork.presentTicks = 0UL;
  Ctrl_DWork.elapsedTicks = 0UL;
  Ctrl_DWork.previousTicks = 0UL;

  /* End of InitializeConditions for SubSystem: '<Root>/Ctrl' */

  /* Enable for Atomic SubSystem: '<Root>/Ctrl' */
  /* Enable for Chart: '<S1>/Chart' */
  Ctrl_DWork.presentTicks = Ctrl_M->Timing.clockTick0;
  Ctrl_DWork.previousTicks = Ctrl_DWork.presentTicks;

  /* End of Enable for SubSystem: '<Root>/Ctrl' */
}

/* Model terminate function */
void CtrlModelClass::terminate()
{
  /* (no terminate code required) */
}

/* Constructor */
CtrlModelClass::CtrlModelClass()
{
  Parameters_Ctrl Ctrl_P_temp = {
    0.0F,                              /* Computed Parameter: Constant1_Value
                                        * Referenced by: '<S3>/Constant1'
                                        */
    0.1F,                              /* Computed Parameter: ProportionalGain_Gain
                                        * Referenced by: '<S8>/Proportional Gain'
                                        */
    5.0E-5F,                           /* Computed Parameter: Integrator_gainval
                                        * Referenced by: '<S8>/Integrator'
                                        */
    0.0F,                              /* Computed Parameter: Integrator_IC
                                        * Referenced by: '<S8>/Integrator'
                                        */
    0.5F,                              /* Computed Parameter: Saturation_UpperSat
                                        * Referenced by: '<S8>/Saturation'
                                        */
    -0.5F,                             /* Computed Parameter: Saturation_LowerSat
                                        * Referenced by: '<S8>/Saturation'
                                        */
    200.0F,                            /* Computed Parameter: IntegralGain_Gain
                                        * Referenced by: '<S8>/Integral Gain'
                                        */
    0.0F,                              /* Computed Parameter: Constant_Value
                                        * Referenced by: '<S8>/Constant'
                                        */
    0.0003F,                           /* Computed Parameter: ProportionalGain_Gain_p
                                        * Referenced by: '<S10>/Proportional Gain'
                                        */
    5.0E-5F,                           /* Computed Parameter: Integrator_gainval_a
                                        * Referenced by: '<S10>/Integrator'
                                        */
    0.0F,                              /* Computed Parameter: Integrator_IC_f
                                        * Referenced by: '<S10>/Integrator'
                                        */
    0.2F,                              /* Computed Parameter: Saturation_UpperSat_m
                                        * Referenced by: '<S10>/Saturation'
                                        */
    -0.2F,                             /* Computed Parameter: Saturation_LowerSat_g
                                        * Referenced by: '<S10>/Saturation'
                                        */
    0.1F,                              /* Computed Parameter: ProportionalGain_Gain_n
                                        * Referenced by: '<S9>/Proportional Gain'
                                        */
    5.0E-5F,                           /* Computed Parameter: Integrator_gainval_j
                                        * Referenced by: '<S9>/Integrator'
                                        */
    0.0F,                              /* Computed Parameter: Integrator_IC_b
                                        * Referenced by: '<S9>/Integrator'
                                        */
    0.85F,                             /* Computed Parameter: Saturation_UpperSat_a
                                        * Referenced by: '<S9>/Saturation'
                                        */
    -0.85F,                            /* Computed Parameter: Saturation_LowerSat_k
                                        * Referenced by: '<S9>/Saturation'
                                        */
    200.0F,                            /* Computed Parameter: IntegralGain_Gain_m
                                        * Referenced by: '<S9>/Integral Gain'
                                        */
    0.0F,                              /* Computed Parameter: Constant_Value_g
                                        * Referenced by: '<S9>/Constant'
                                        */
    0.0F,                              /* Computed Parameter: IntegralGain_Gain_i
                                        * Referenced by: '<S10>/Integral Gain'
                                        */
    0.0F,                              /* Computed Parameter: Constant_Value_gp
                                        * Referenced by: '<S10>/Constant'
                                        */
    1.0F,                              /* Computed Parameter: StupidMerge_Gain
                                        * Referenced by: '<S3>/StupidMerge'
                                        */
    1.0F,                              /* Computed Parameter: StupidMerge1_Gain
                                        * Referenced by: '<S3>/StupidMerge1'
                                        */
    0.0F,                              /* Computed Parameter: Constant1_Value_m
                                        * Referenced by: '<S4>/Constant1'
                                        */
    0.1F,                              /* Computed Parameter: ProportionalGain_Gain_e
                                        * Referenced by: '<S14>/Proportional Gain'
                                        */
    5.0E-5F,                           /* Computed Parameter: Integrator_gainval_g
                                        * Referenced by: '<S14>/Integrator'
                                        */
    0.0F,                              /* Computed Parameter: Integrator_IC_a
                                        * Referenced by: '<S14>/Integrator'
                                        */
    0.5F,                              /* Computed Parameter: Saturation_UpperSat_l
                                        * Referenced by: '<S14>/Saturation'
                                        */
    -0.5F,                             /* Computed Parameter: Saturation_LowerSat_a
                                        * Referenced by: '<S14>/Saturation'
                                        */
    200.0F,                            /* Computed Parameter: IntegralGain_Gain_mu
                                        * Referenced by: '<S14>/Integral Gain'
                                        */
    0.0F,                              /* Computed Parameter: Constant_Value_c
                                        * Referenced by: '<S14>/Constant'
                                        */
    0.1F,                              /* Computed Parameter: ProportionalGain_Gain_b
                                        * Referenced by: '<S15>/Proportional Gain'
                                        */
    5.0E-5F,                           /* Computed Parameter: Integrator_gainval_an
                                        * Referenced by: '<S15>/Integrator'
                                        */
    0.0F,                              /* Computed Parameter: Integrator_IC_j
                                        * Referenced by: '<S15>/Integrator'
                                        */
    0.85F,                             /* Computed Parameter: Saturation_UpperSat_b
                                        * Referenced by: '<S15>/Saturation'
                                        */
    -0.85F,                            /* Computed Parameter: Saturation_LowerSat_e
                                        * Referenced by: '<S15>/Saturation'
                                        */
    200.0F,                            /* Computed Parameter: IntegralGain_Gain_h
                                        * Referenced by: '<S15>/Integral Gain'
                                        */
    0.0F,                              /* Computed Parameter: Constant_Value_k
                                        * Referenced by: '<S15>/Constant'
                                        */
    1.0F,                              /* Computed Parameter: StupidMerge_Gain_c
                                        * Referenced by: '<S4>/StupidMerge'
                                        */
    1.0F,                              /* Computed Parameter: StupidMerge1_Gain_l
                                        * Referenced by: '<S4>/StupidMerge1'
                                        */
    0.0F,                              /* Computed Parameter: Constant1_Value_k
                                        * Referenced by: '<S5>/Constant1'
                                        */
    0.0F,                              /* Computed Parameter: Constant2_Value
                                        * Referenced by: '<S5>/Constant2'
                                        */
    0.0F,                              /* Computed Parameter: Constant3_Value
                                        * Referenced by: '<S5>/Constant3'
                                        */
    5.0E-5F,                           /* Computed Parameter: DiscreteTimeIntegrator_gainval
                                        * Referenced by: '<S18>/Discrete-Time Integrator'
                                        */
    0.0F,                              /* Computed Parameter: DiscreteTimeIntegrator_IC
                                        * Referenced by: '<S18>/Discrete-Time Integrator'
                                        */
    1.0F,                              /* Computed Parameter: Constant_Value_p
                                        * Referenced by: '<S21>/Constant'
                                        */
    0.05F,                             /* Computed Parameter: RateLimiter_RisingLim
                                        * Referenced by: '<S18>/Rate Limiter'
                                        */
    -0.05F,                            /* Computed Parameter: RateLimiter_FallingLim
                                        * Referenced by: '<S18>/Rate Limiter'
                                        */
    0.0F,                              /* Computed Parameter: RateLimiter_IC
                                        * Referenced by: '<S18>/Rate Limiter'
                                        */
    0.0166666675F,                     /* Computed Parameter: rps_Gain
                                        * Referenced by: '<S18>/rps'
                                        */
    7.0F,                              /* Computed Parameter: rps1_Gain
                                        * Referenced by: '<S18>/rps1'
                                        */
    0.0F,                              /* Computed Parameter: Constant_Value_ka
                                        * Referenced by: '<S22>/Constant'
                                        */
    1.0F,                              /* Computed Parameter: Saturation_UpperSat_f
                                        * Referenced by: '<S18>/Saturation'
                                        */
    0.0F,                              /* Computed Parameter: Saturation_LowerSat_gl
                                        * Referenced by: '<S18>/Saturation'
                                        */
    0.0F,                              /* Computed Parameter: Constant1_Value_e
                                        * Referenced by: '<S6>/Constant1'
                                        */
    0.1F,                              /* Computed Parameter: ProportionalGain_Gain_i
                                        * Referenced by: '<S19>/Proportional Gain'
                                        */
    5.0E-5F,                           /* Computed Parameter: Integrator_gainval_e
                                        * Referenced by: '<S19>/Integrator'
                                        */
    0.0F,                              /* Computed Parameter: Integrator_IC_m
                                        * Referenced by: '<S19>/Integrator'
                                        */
    0.5F,                              /* Computed Parameter: Saturation_UpperSat_d
                                        * Referenced by: '<S19>/Saturation'
                                        */
    -0.5F,                             /* Computed Parameter: Saturation_LowerSat_ej
                                        * Referenced by: '<S19>/Saturation'
                                        */
    200.0F,                            /* Computed Parameter: IntegralGain_Gain_l
                                        * Referenced by: '<S19>/Integral Gain'
                                        */
    0.0F,                              /* Computed Parameter: Constant_Value_p0
                                        * Referenced by: '<S19>/Constant'
                                        */
    0.1F,                              /* Computed Parameter: ProportionalGain_Gain_a
                                        * Referenced by: '<S20>/Proportional Gain'
                                        */
    5.0E-5F,                           /* Computed Parameter: Integrator_gainval_i
                                        * Referenced by: '<S20>/Integrator'
                                        */
    0.0F,                              /* Computed Parameter: Integrator_IC_n
                                        * Referenced by: '<S20>/Integrator'
                                        */
    0.85F,                             /* Computed Parameter: Saturation_UpperSat_as
                                        * Referenced by: '<S20>/Saturation'
                                        */
    -0.85F,                            /* Computed Parameter: Saturation_LowerSat_av
                                        * Referenced by: '<S20>/Saturation'
                                        */
    200.0F,                            /* Computed Parameter: IntegralGain_Gain_le
                                        * Referenced by: '<S20>/Integral Gain'
                                        */
    0.0F,                              /* Computed Parameter: Constant_Value_cy
                                        * Referenced by: '<S20>/Constant'
                                        */
    1.0F,                              /* Computed Parameter: StupidMerge_Gain_n
                                        * Referenced by: '<S6>/StupidMerge'
                                        */
    1.0F,                              /* Computed Parameter: StupidMerge1_Gain_e
                                        * Referenced by: '<S6>/StupidMerge1'
                                        */
    5.0E-5F,                           /* Computed Parameter: DiscreteTimeIntegrator_gainva_h
                                        * Referenced by: '<S25>/Discrete-Time Integrator'
                                        */
    0.0F,                              /* Computed Parameter: DiscreteTimeIntegrator_IC_k
                                        * Referenced by: '<S25>/Discrete-Time Integrator'
                                        */
    1.0F,                              /* Computed Parameter: Constant_Value_j
                                        * Referenced by: '<S26>/Constant'
                                        */
    0.05F,                             /* Computed Parameter: RateLimiter_RisingLim_e
                                        * Referenced by: '<S25>/Rate Limiter'
                                        */
    -0.05F,                            /* Computed Parameter: RateLimiter_FallingLim_d
                                        * Referenced by: '<S25>/Rate Limiter'
                                        */
    0.0F,                              /* Computed Parameter: RateLimiter_IC_f
                                        * Referenced by: '<S25>/Rate Limiter'
                                        */
    0.0166666675F,                     /* Computed Parameter: rps_Gain_d
                                        * Referenced by: '<S25>/rps'
                                        */
    7.0F,                              /* Computed Parameter: rps1_Gain_i
                                        * Referenced by: '<S25>/rps1'
                                        */
    0.0F,                              /* Computed Parameter: Constant_Value_f
                                        * Referenced by: '<S27>/Constant'
                                        */
    1.0F,                              /* Computed Parameter: Saturation_UpperSat_i
                                        * Referenced by: '<S25>/Saturation'
                                        */
    0.0F,                              /* Computed Parameter: Saturation_LowerSat_gd
                                        * Referenced by: '<S25>/Saturation'
                                        */
    0.0F,                              /* Computed Parameter: Constant1_Value_h
                                        * Referenced by: '<S7>/Constant1'
                                        */
    156U,                              /* Computed Parameter: Gain_Gain
                                        * Referenced by: '<S18>/Gain'
                                        */
    156U,                              /* Computed Parameter: Gain1_Gain
                                        * Referenced by: '<S18>/Gain1'
                                        */
    156U,                              /* Computed Parameter: Gain_Gain_b
                                        * Referenced by: '<S25>/Gain'
                                        */
    156U                               /* Computed Parameter: Gain1_Gain_j
                                        * Referenced by: '<S25>/Gain1'
                                        */
  };                                   /* Modifiable parameters */

  /* Initialize tunable parameters */
  Ctrl_P = Ctrl_P_temp;

  /* Real-Time Model */
  Ctrl_M = &Ctrl_M_;
}

/* Destructor */
CtrlModelClass::~CtrlModelClass()
{
  /* Currently there is no destructor body generated.*/
}

/* Real-Time Model get method */
RT_MODEL_Ctrl * CtrlModelClass::getRTM() const
{
  return Ctrl_M;
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
